package com.nostra13.example.universalimageloader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class HomeActivity extends BaseActivity {

	private String[] imageUrls;
	private String[] previewImageUrls;

	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//String[] heavyImages = getResources().getStringArray(R.array.heavy_images);
		ImageExtractor imageExtractor = new ImageExtractor("http://parovoz.com/newgallery");
		
		Collection<String> heavyImages = null;
		Collection<String> lightImages = null;
		Exception exc = null;
		try {
			Map<String, String> images = imageExtractor.getImages();
			lightImages = images.keySet();
			heavyImages = images.values();
			imageUrls = (String[]) heavyImages.toArray(new String[0]);
			previewImageUrls = (String[]) lightImages.toArray(new String[0]);
		} catch (IOException e) {
			exc = e;
			Log.e("catched_error", e.getMessage(), e);
		}
		if (exc != null) {
		    
			TextView noConnectTextView = (TextView) findViewById(R.id.no_connect_message);
			noConnectTextView.setText(exc.getMessage());
		}
		else
			setContentView(R.layout.ac_home);
	}
	
	private static boolean isDataConnectionAvailable(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if(info == null)
            return false;

        return connectivityManager.getActiveNetworkInfo().isConnected();
    }

	public void onImageListClick(View view) {
		Intent intent = new Intent(this, ImageListActivity.class);
		intent.putExtra(Extra.IMAGES, imageUrls);
		startActivity(intent);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.item_clear_memory_cache:
				imageLoader.clearMemoryCache();
				return true;
			case R.id.item_clear_disc_cache:
				imageLoader.clearDiscCache();
				return true;
			default:
				return false;
		}
	}
	
	public void onImageGridClick(View view) {
		Intent intent = new Intent(this, ImageGridActivity.class);
		intent.putExtra(Extra.IMAGES, imageUrls);
		intent.putExtra(Extra.PREVIEW_IMAGES, previewImageUrls);
		
		startActivity(intent);
	}

	public void onImagePagerClick(View view) {
		Intent intent = new Intent(this, ImagePagerActivity.class);
		intent.putExtra(Extra.IMAGES, imageUrls);
		startActivity(intent);
	}

	public void onImageGalleryClick(View view) {
		Intent intent = new Intent(this, ImageGalleryActivity.class);
		intent.putExtra(Extra.IMAGES, imageUrls);
		startActivity(intent);
	}
}